<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Controller{

	public function __construct(){
		parent::__construct();
	}
	
	public function ww(){
		$this->load->library('Wework/wework', [
			'corpsecret' => 'pjQrOogM4feQ_oqbnZ697Civt5kGR7P3dAdUCFo-O1Y'
		]);
		$res = $this->wework->get_userinfo_bycode();
		// echo json_encode($res);
		echo json_encode($this->wework->user_get($res['UserId']));
		/* $res = $this->wework->user_get($res['UserId']);
		echo json_encode($res);
		echo json_encode($this->wework->userid_to_openid($res['UserId']));
		echo json_encode($this->wework->error); */
	}
	
	public function a(){
		$this->load->library('Wechat/wechatjs');
		$openid = $this->wechatjs->get_openid();
		echo $openid;
	}
	
	public function sms(){
		$this->load->library('http');
		$data = [
			'ecName' => '十堰辰育教育集团有限公司',
			'apId' => 'cywl',
			'templateId' => 'c649662715a942ce9f3e631240a0293c',
			// 'mobiles' => '13907150323',
			'mobiles' => '13294212321',
			'params' => ['123456'],
			'sign' => 'GhJpW97BA',
			'addSerial' => ''
		];
		$mac_str = '';
		$i = 0;
		foreach($data as $value){
			if($i == 2){
				$mac_str .= 'Cywl654123';
			}
			if(is_array($value)){
				$value = json_encode($value);
			}
			$mac_str .= $value;
			$i++;
		}
		$data['mac'] = strtolower(md5($mac_str));
		
		/* print_r($data);
		echo 'mac_str: '.$mac_str."\r\n"; */
		$data = base64_encode(json_encode($data));
		/* echo 'data: '.$data;
		exit; */
		$res = $this->http->request('http://112.35.10.201:5992/sms/tmpsubmit', $data, 'POST');
		var_dump($res);
	}
	
	public function s(){
		$this->load->library('myexcel');
		$file = ROOTPATH.'/s.xlsx';
		$res = $this->myexcel->read($file);
		if(0 !== $res['error']){
			echo json_encode($res);
			exit;
		}
		$rows = $res['data'];
		
		$this->load->model(['school_model', 'grade_model', 'campus_model', 'signup_record_model']);
		
		$school_arr = $this->school_model->get_result();
		$school_arr = array_column($school_arr, 'title', 'id');
		
		$grade_arr = $this->grade_model->get_result();
		$grade_arr = array_column($grade_arr, 'title', 'id');
		
		$campus_arr = $this->campus_model->get_result();
		$campus_arr = array_column($campus_arr, 'title', 'id');
		
		$this->load->library('form_validation');
		
		$data = [];
		$errdata = [];
		
		foreach($rows as $row){
			$idno = $row['G'];
			
			// 性别 0=女、1=男
			$sex = substr($idno, 16, 1) % 2;
			$sex = ($sex !== 0) ? 1 : 0;
			
			// 生日
			$birthday = substr($idno, 6, 8);
			
			$newrow = [
				'school_id' => $row['L'],
				'school' => $school_arr[$row['L']],
				'grade_id' => $row['M'],
				'grade' => $grade_arr[$row['M']],
				'campus_id' => $row['N'],
				'campus' => $campus_arr[$row['N']],
				'student_name' => $row['D'],
				'student_idno' => $idno,
				'student_sex' => $sex,
				'student_birthday' => date('Y-m-d', strtotime($birthday)),
				// 'nation' => '',
				// 'height' => '',
				// 'weight' => '',
				'live_address' => $row['H'],
				// 'family_address' => '',
				'now_school' => $row['I'],
				'now_grade' => $row['J'],
				'interested' => $row['P'],
				// 'honors' => $row['honors'],
				// 'father_name' => $row['father_name'],
				// 'father_phone' => $row['father_phone'],
				// 'father_company' => $row['father_company'],
				// 'mother_name' => $row['mother_name'],
				// 'mother_phone' => $row['mother_phone'],
				// 'mother_company' => $row['mother_company'],
				'parent_name' => $row['Q'],
				'parent_phone' => $row['R'],
				// 'parent_clan' => $row['parent_clan'],
				'parent_email' => strtolower($row['S']),
				'follow_way' => implode(',', unserialize($row['T'])),
				'remark' => $row['V'],
				'inputtime' => strtotime($row['C'])
			];
			$this->form_validation->reset_validation();
			$this->form_validation->set_data($newrow);
			$this->form_validation->set_rules('student_idno', '身份证号', 'required|valid_idcard');
			$this->form_validation->set_rules('parent_phone', '家长手机号', 'required|valid_phone');
			if($this->form_validation->run() == false){
				$errdata[] = [
					'row_index' => $row['A'],
					'name' => $newrow['student_name'],
					'idno' => $newrow['student_idno'],
					'parent_phone' => $newrow['parent_phone'],
					'error' => $this->form_validation->error_array()
				];
			}else{
				if($newrow['school_id'] == 1 || $newrow['school_id'] == 2){
					$newrow['status'] = 1;
				}
				$data[] = $newrow;
			}
		}
		// echo json_encode($errdata);
		// print_r($data);
		
		$idno_arr = array_column($data, 'student_idno');
		$idno_arr = array_flip($idno_arr);
		$idno_arr = array_keys($idno_arr);
		print_r($idno_arr);
		
		/* $total = count($data);
		for($i = 0; $i < $total; $i++){
			$item = $data[$i];
			$signed = $this->signup_record_model->get_row([
				'student_idno' => $item['student_idno']
			]);
			if($signed){
				unset($data[$i]);
			}
		} */
		
		echo json_encode([
			'total' => $total,
			'valid_num' => count($data)
		]);
	}
	
}